package com.zhentao.controller;

import com.zhentao.config.Result;
import com.zhentao.pojo.TbMessage;
import com.zhentao.service.TbMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message")
public class TbMessageController {

    @Autowired
    private TbMessageService tbMessageService;

    /**
     * 获取接收者的消息列表
     * receiverType: 1学生 2教师
     */
    @GetMapping("/list")
    public Result<List<TbMessage>> list(@RequestParam("receiverId") Long receiverId,
                                        @RequestParam("receiverType") Integer receiverType) {
        List<TbMessage> list = tbMessageService.listByReceiver(receiverId, receiverType);
        return Result.success(list);
    }

    /**
     * 发送消息
     */
    @PostMapping("/send")
    public Result<String> send(@RequestBody TbMessage message) {
        boolean ok = tbMessageService.sendMessage(message);
        return ok ? Result.success("发送成功") : Result.error("发送失败");
    }

    /**
     * 标记已读
     */
    @PostMapping("/read")
    public Result<String> markRead(@RequestParam("id") Long id) {
        boolean ok = tbMessageService.markRead(id);
        return ok ? Result.success("已标记为已读") : Result.error("标记失败");
    }
}